package com.kele.controller;

import com.kele.domain.User;
import com.kele.domain.messages;
import com.kele.service.MessagesService;
import com.kele.service.UserService;
import com.kele.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * 创作人 王箫凯
 * Date 2021/6/14
 * Time 15:26
 *
 * @author 王箫凯
 */

@Controller
@RequestMapping("/message")
@Api(value = "消息模块",tags = "消息管理界面")
public class messageBox {
    @Resource
    MessagesService messagesService;

    @Resource
    UserService userService;



    /**
     * 创建房间，并邀请人加入
     * @param request 获取session
     * @param account 获取被邀请人的账号
     * @param code 状态码，默认为0，表示审核状态，
     * @return JS 实现消息通知
     */
    @PostMapping("/createRoom")
    @ResponseBody
    @ApiOperation(value = "创建一个房间",notes = "创建房间")
    public ResponseData createRoom(HttpServletRequest request,
                                   @RequestParam(value = "account") String account,
                                   @RequestParam(value = "code",defaultValue = "0") int code){
        User user= (User) request.getSession().getAttribute("User");
        //创建一个UUID 作为房间号
        String roomNumber=UUID.randomUUID().toString().replaceAll("-","");
        //预先创建一个房间
        int creatRoom= messagesService.createChatRoom(user.getAccount(),roomNumber);
        //将另一位同志拉进房间
        creatRoom+=messagesService.createChatRoom(account,roomNumber);

        //邀请这位同志进入房间
//        int invitedJoin=messagesService.invitedUserJoin(account,roomNumber,code);
//        int count=creatRoom+invitedJoin;
        if (creatRoom==2){
            return ResponseData.success("房间创建成功","房间号为："+roomNumber);
        }else {
            throw new RuntimeException("未知错误");
        }

    }

    @PostMapping("/doInvited")
    @ResponseBody
    @ApiOperation(value = "接受或者拒绝一个邀请",notes = "接受或者拒绝一个情求")
    public ResponseData doInvited(@RequestParam("roomNumber") String roomNumber,
                                  HttpServletRequest request,
                                  @RequestParam("code") int code){

        User user= (User) request.getSession().getAttribute("User");

        int isHaveRoom= messagesService.selectRoom(user.getAccount(),roomNumber);
        if (isHaveRoom==0){
            return ResponseData.fail("该对话已取消，或者不存在！");
        }
        if (messagesService.invitedCode(user.getAccount(),roomNumber)==0){
            return ResponseData.fail("您已经处理过该请求了");
        }

        int count=0;
        //表示同意加入
        if (code==1){
            count=count+messagesService.createChatRoom(user.getAccount(),roomNumber);
            count=+messagesService.doInvited(user.getAccount(),roomNumber,code);

            return ResponseData.success("已加入房号为："+roomNumber+"的房间");
        }



            return ResponseData.fail("您已拒绝了该邀请");


    }

    /**
     * 发送一个私信给某个人
     * @param message 需要发送
     * @param roomNumber
     * @param request
     * @return
     */
    @PostMapping("/sendMessages")
    @ResponseBody
    @ApiOperation(value = "发送信息",notes = "发送信息操作")
    public ResponseData sendMessage(@RequestParam("sendMessages") String message,
                                    @RequestParam("roomNumber") String roomNumber,
                                    HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("User");



        String myAccount=user.getAccount();
        String myNickName=user.getNickname();
        int nums= messagesService.sendMessages(message,roomNumber,myAccount,myNickName);
        if (nums>=1){
            return ResponseData.success("发送成功");
        }
        return ResponseData.fail("未知错误");

    }
    //遍历自己的所有信息

    /**
     * 查找自己所有聊天室
     * @param request 获取session
     * @return 返回一个list集合
     */
    @ApiOperation(value = "检测自己的聊天对象的个数",notes = "检测自己的聊天对象的个数")
    @GetMapping("/myChat")
    public String myMessages(HttpServletRequest request, Model model){
        User user = (User) request.getSession().getAttribute("User");
        String myAccount=user.getAccount();
        List<messages> list=messagesService.myMessages(myAccount);
        System.out.println("============"+list);
        model.addAttribute("list",list);
        return "static_resources/templates/privateMessage";
    }

    /**
     * 查看自己的聊天室的内容
     * @param roomNumber 房间号
     * @param request 获取session
     * @return 理论上应当返回一个页面 用于显示，也可以使用Ajax来接收到这个数据，
     * 使得它可以一直往下翻或者说可以通过它进行遍历
     */
    @GetMapping("/messages")
    @ApiOperation(value = "获取自己的信息",notes = "查看自己聊天室的记录")
    @ResponseBody
    public List<messages> selectMessageWith(@RequestParam(value = "roomNumber") String roomNumber,
                                            HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("User");
        String myAccount=user.getAccount();
        List<messages> list=messagesService.selectMessagesWith(roomNumber,myAccount);
        System.out.println(list);
        return list;
    }


    /**
     * 删除自己的某个私信，本质上就是在自己这里删除这个聊天室
     * @param request 获取session，得到User
     * @param roomNumber 房间号，房间号具有唯一表示的作用
     * @return 返回一个Ajax
     */
    @PostMapping("/delectRoom")
    @ApiOperation(value = "删除自己的聊天室",notes = "删除与某位用户的私信")
    @ResponseBody
    public ResponseData deleteChatRoom(HttpServletRequest request,
                                       @RequestParam("roomNumber") String roomNumber){
    User user= (User) request.getSession().getAttribute("User");
    String myAccount=user.getAccount();
    int nums=messagesService.deleteMessageRoom(roomNumber,myAccount);
//    if (nums<=0){
//        throw new RuntimeException("未知异常");
//    }
    return ResponseData.success("操作成功",nums);
    }


}
